package com.ling.tts3;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ling.tts3.utils.TTSUtil;

import java.util.HashMap;
import java.util.Locale;

public class TTS1Activity extends AppCompatActivity {

    private static final int MY_DATA_CHECK_CODE = 100;
    private static String TAG = "TTS1Activity";
    // tts
//    private TTSUtil ttsUtil;

    private TextToSpeech tts;
    private float defaultSpeechRate = 1.0f;  // 默认语速
    private float defaultPitch = 1.0f;       // 默认音调
        private Locale defaultLocale = Locale.CHINESE; // 默认语言
//    private Locale defaultLocale = Locale.US; // 默认语言
    private String utteranceId = "utteranceId"; // 唯一标识符

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_tts1);
        Button button = findViewById(R.id.test);
        TextView textView = findViewById(R.id.edit);

//        button.setOnClickListener(v -> {
//            String text = textView.getText().toString();
//            if (text.isEmpty()) {
//                Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
//            }
//        });
        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    Log.d(TAG,"TTS 引擎初始化成功:"+status);
                    int result = tts.setLanguage(Locale.CHINESE); // 设置语言，例如美国英语
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.d(TAG,"TTS 语言数据缺失或不支持:"+result);
                        // 语言数据缺失或不支持
                        Toast.makeText(TTS1Activity.this, "语言数据缺失或不支持", Toast.LENGTH_SHORT).show();
                    } else {
                        // 成功初始化 TTS 引擎
                        Toast.makeText(TTS1Activity.this, "TTS 引擎初始化成功", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Log.d(TAG,"TTS 引擎初始化失败:"+status);
                    // 初始化失败
                    Toast.makeText(TTS1Activity.this, "TTS 引擎初始化失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        button.setOnClickListener(v -> {
            String text = textView.getText().toString();
            Log.d(TAG,"转换:"+text);
            if (!text.isEmpty()) {
                Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
                tts.setLanguage(defaultLocale);// 设置语言
                tts.setSpeechRate(defaultSpeechRate);// 设置语速
                tts.setPitch(defaultPitch);// 设置音调

                Bundle params = new Bundle();
                params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
//                TextToSpeech.Engine.KEY_PARAM_STREAM
//                TextToSpeech.Engine.KEY_PARAM_VOLUME
//                TextToSpeech.Engine.KEY_PARAM_PAN.
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, utteranceId);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // 数据已安装，可以正常使用 TTS
                Log.i(TAG,"TTS 数据已安装，可以正常使用 TTS");
            } else {
                // 数据未安装，提示用户安装或处理其他逻辑
                Log.i(TAG,"TTS 数据未安装，提示用户安装或处理其他逻辑");
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

    // 销毁 TTS 资源
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
    }
}